home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / Sample.bin / jre_md.c < prev    next >
C/C++ Source or Header  |  1998-09-15  |  7KB  |  272 lines

  1. /*
  2.  * @(#)jre_md.c    1.7 98/07/01
  3.  *
  4.  * Copyright 1997, 1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  * 
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. /*
  16.  * Win32 specific JRE support functions
  17.  */
  18.  
  19. #include <windows.h>
  20. #include <stdlib.h>
  21. #include <jni.h>
  22. #include "jre.h"
  23.  
  24. #define JRE_KEY        "Software\\JavaSoft\\Java Runtime Environment"
  25. #define JDK_KEY        "Software\\JavaSoft\\Java Development Kit"
  26.  
  27. #define RUNTIME_LIB "javai.dll"
  28.  
  29. /* From jre_main.c */
  30. extern jboolean debug;
  31.  
  32. /* Forward Declarations */
  33. jint LoadSettings(JRESettings *set, HKEY key);
  34. jint GetSettings(JRESettings *set, const char *version, const char *keyname);
  35. char *GetStringValue(HKEY key, const char *name);
  36.  
  37. /*
  38.  * Retrieve settings from registry for current runtime version. Returns
  39.  * 0 if successful otherwise returns -1 if no installed runtime was found
  40.  * or the registry data was invalid.
  41.  */
  42. jint
  43. JRE_GetCurrentSettings(JRESettings *set)
  44. {
  45.     jint r = -1;
  46.     HKEY key;
  47.  
  48.     if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, JRE_KEY, 0, KEY_READ, &key) == 0) {
  49.     char *ver = GetStringValue(key, "CurrentVersion");
  50.     if (ver != 0) {
  51.         r = JRE_GetSettings(set, ver);
  52.     }
  53.     free(ver);
  54.     RegCloseKey(key);
  55.     }
  56.     return r;
  57. }
  58.  
  59. /*
  60.  * Retrieves settings from registry for specified runtime version.
  61.  * Searches for either installed JRE and JDK runtimes. Returns 0 if
  62.  * successful otherwise returns -1 if requested version of runtime
  63.  * could not be found.
  64.  */
  65. jint
  66. JRE_GetSettings(JRESettings *set, const char *version)
  67. {
  68.     if (GetSettings(set, version, JRE_KEY) != 0) {
  69.     return GetSettings(set, version, JDK_KEY);
  70.     }
  71.     return 0;
  72. }
  73.  
  74. jint
  75. GetSettings(JRESettings *set, const char *version, const char *keyname)
  76. {
  77.     HKEY key;
  78.     int r = -1;
  79.  
  80.     if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, keyname, 0, KEY_READ, &key) == 0) {
  81.     char *major, *minor, *micro = 0;
  82.     if (JRE_ParseVersion(version, &major, &minor, µ) == 0) {
  83.         HKEY subkey;
  84.         char *ver = JRE_MakeVersion(major, minor, 0);
  85.         set->majorVersion = major;
  86.         set->minorVersion = minor;
  87.         if (RegOpenKeyEx(key, ver, 0, KEY_READ, &subkey) == 0) {
  88.         if ((r = LoadSettings(set, subkey)) == 0) {
  89.             if (micro != 0) {
  90.             if (set->microVersion == 0 ||
  91.                 strcmp(micro, set->microVersion) != 0) {
  92.                 r = -1;
  93.             }
  94.             }
  95.         }
  96.         RegCloseKey(subkey);
  97.         }
  98.         free(ver);
  99.     }
  100.     RegCloseKey(key);
  101.     }
  102.     return r;
  103. }
  104.  
  105. /*
  106.  * Load runtime settings from specified registry key. Returns 0 if
  107.  * successful otherwise -1 if the registry data was invalid.
  108.  */
  109. static jint
  110. LoadSettings(JRESettings *set, HKEY key)
  111. {
  112.     /* Full path name of JRE home directory (required) */
  113.     set->javaHome = GetStringValue(key, "JavaHome");
  114.     if (set->javaHome == 0) {
  115.     return -1;
  116.     }
  117.     /* Full path name of JRE runtime DLL */
  118.     set->runtimeLib = GetStringValue(key, "RuntimeLib");
  119.     if (set->runtimeLib == 0) {
  120.     set->runtimeLib = JRE_GetDefaultRuntimeLib(set->javaHome);
  121.     }
  122.     /* Class path setting to override default */
  123.     set->classPath = GetStringValue(key, "ClassPath");
  124.     if (set->classPath == 0) {
  125.     set->classPath = JRE_GetDefaultClassPath(set->javaHome);
  126.     }
  127.     /* Optional JIT compiler library name */
  128.     set->compiler = GetStringValue(key, "Compiler");
  129.     /* Release micro-version */
  130.     set->microVersion = GetStringValue(key, "MicroVersion");
  131.     return 0;
  132. }
  133.  
  134. /*
  135.  * Returns string data for the specified registry value name, or
  136.  * NULL if not found.
  137.  */
  138. static char *
  139. GetStringValue(HKEY key, const char *name)
  140. {
  141.     DWORD type, size;
  142.     char *value = 0;
  143.  
  144.     if (RegQueryValueEx(key, name, 0, &type, 0, &size) == 0 &&
  145.     type == REG_SZ ) {
  146.     value = JRE_Malloc(size);
  147.     if (RegQueryValueEx(key, name, 0, 0, value, &size) != 0) {
  148.         free(value);
  149.         value = 0;
  150.     }
  151.     }
  152.     return value;
  153. }
  154.  
  155. /*
  156.  * Returns default runtime settings based on location of this program.
  157.  * Makes best attempt at determining location of runtime. Returns 0
  158.  * if successful or -1 if a runtime could not be found.
  159.  */
  160. jint
  161. JRE_GetDefaultSettings(JRESettings *set)
  162. {
  163.     char buf[MAX_PATH], *bp;
  164.     int n;
  165.  
  166.     // Try to obtain default value for Java home directory based on
  167.     // location of this executable.
  168.  
  169.     if ((n = GetModuleFileName(0, buf, MAX_PATH)) == 0) {
  170.     return -1;
  171.     }
  172.     bp = buf + n;
  173.     while (*--bp != '\\') ;
  174.     bp -= 4;
  175.     if (bp < buf || strnicmp(bp, "\\bin", 4) != 0) {
  176.     return -1;
  177.     }
  178.     *bp = '\0';
  179.     set->javaHome = strdup(buf);
  180.  
  181.     // Get default runtime library
  182.     set->runtimeLib = JRE_GetDefaultRuntimeLib(set->javaHome);
  183.  
  184.     // Get default class path
  185.     set->classPath = JRE_GetDefaultClassPath(set->javaHome);
  186.  
  187.     // Reset other fields since these are unknown
  188.     set->compiler = 0;
  189.     set->majorVersion = 0;
  190.     set->minorVersion = 0;
  191.     set->microVersion = 0;
  192.  
  193.     return 0;
  194. }
  195.  
  196. /*
  197.  * Return default runtime library for specified Java home directory.
  198.  */
  199. char *
  200. JRE_GetDefaultRuntimeLib(const char *dir)
  201. {
  202.     char *cp = JRE_Malloc(strlen(dir) + sizeof(RUNTIME_LIB) + 8);
  203.     sprintf(cp, "%s\\bin\\" RUNTIME_LIB, dir);
  204.     return cp;
  205. }
  206.  
  207. /*
  208.  * Return default class path for specified Java home directory.
  209.  */
  210. char *
  211. JRE_GetDefaultClassPath(const char *dir)
  212. {
  213.     char *cp = JRE_Malloc(strlen(dir) * 4 + 64);
  214.     sprintf(cp, "%s\\lib\\rt.jar;%s\\lib\\i18n.jar;%s\\lib\\classes.zip;"
  215.         "%s\\classes", dir, dir, dir, dir);
  216.     return cp;
  217. }
  218.  
  219. /*
  220.  * Loads the runtime library corresponding to 'libname' and returns
  221.  * an opaque handle to the library.
  222.  */
  223. void *
  224. JRE_LoadLibrary(const char *path)
  225. {
  226.     return (void *)LoadLibrary(path);
  227. }
  228.  
  229. /*
  230.  * Unloads the runtime library associated with handle.
  231.  */
  232. void
  233. JRE_UnloadLibrary(void *handle)
  234. {
  235.     FreeLibrary(handle);
  236. }
  237.  
  238. /*
  239.  * Loads default VM args for the specified runtime library handle.
  240.  */
  241. jint
  242. JRE_GetDefaultJavaVMInitArgs(void *handle, void *vmargs)
  243. {
  244.     FARPROC proc = GetProcAddress(handle, "JNI_GetDefaultJavaVMInitArgs");
  245.     return proc != 0 ? ((*proc)(vmargs), 0) : -1;
  246. }
  247.  
  248. /*
  249.  * Creates a Java VM for the specified runtime library handle.
  250.  */
  251. jint
  252. JRE_CreateJavaVM(void *handle, JavaVM **vmp, JNIEnv **envp, void *vmargs)
  253. {
  254.     FARPROC proc = GetProcAddress(handle, "JNI_CreateJavaVM");
  255.     return proc != 0 ? (*proc)(vmp, envp, vmargs) : -1;
  256. }
  257.  
  258. /*
  259.  * Entry point for JREW (Windows-only) version of the runtime loader.
  260.  * This entry point is called when the '-subsystem:windows' linker
  261.  * option is used, and will cause the resulting executable to run
  262.  * detached from the console.
  263.  */
  264. int WINAPI
  265. WinMain(HINSTANCE inst, HINSTANCE prevInst, LPSTR cmdLine, int cmdShow)
  266. {
  267.     __declspec(dllimport) char **__initenv;
  268.  
  269.     __initenv = _environ;
  270.     exit(main(__argc, __argv));
  271. }
  272.